package com.yanqu.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CollectStatistic {

    private static final Logger logger = LogManager.getLogger("COLLECT");

    private static final Logger logger2 = LogManager.getLogger(CollectDataController.class.getName());

    private static Map<Long, CollectInfo> receiveSizeMap = new ConcurrentHashMap<>();

    private static volatile int lastMinute = Calendar.getInstance().get(Calendar.MINUTE);

    public static void addReceive(long serverId, long size) {
        try{
            int curMinute = Calendar.getInstance().get(Calendar.MINUTE);
            if (isMinuteGap(curMinute)) {
                synchronized (receiveSizeMap) {
                    if (isMinuteGap(curMinute)) {

                        //排序，打印出前20
                        logger.info("================");
                        logger.info("CollectStatistic: {}", lastMinute);
                        List<CollectInfo> list = new ArrayList<>(receiveSizeMap.values());
                        list.sort((o1, o2) -> Long.compare(o2.getSize(), o1.getSize()));

                        int num = 0;
                        long totalNum = 0;
                        long totalTimes = 0;
                        for (CollectInfo info : list) {
                            if (info.getSize() <= 0) {
                                continue;
                            }
                            num++;
                            if (num <= 10) {
                                //耗时前10
                                logger.info("区服 {} 收集大小 {}", info.getServerId(), info.getSize() / 1024);
                            }
                            totalNum += info.getSize();
                            totalTimes += info.getTimes();
                        }

                        logger.info("总收集大小 {} k, 总请求次数 {}", totalNum / 1024, totalTimes);

                        //清空
                        for (Map.Entry<Long, CollectInfo> entry : receiveSizeMap.entrySet()) {
                            entry.getValue().setSize(0L);
                            entry.getValue().setTimes(0);
                        }

                        lastMinute = curMinute;
                    }
                }
            }

            CollectInfo collectInfo = receiveSizeMap.get(serverId);
            if (collectInfo == null) {
                synchronized (receiveSizeMap) {
                    collectInfo = receiveSizeMap.get(serverId);
                    if (collectInfo == null) {
                        collectInfo = new CollectInfo(serverId, 0L);
                        receiveSizeMap.put(serverId, collectInfo);
                    }
                }
            }

            synchronized (collectInfo) {
                collectInfo.addSize(size);
                collectInfo.addTimes(1);
            }
        }catch (Exception e){
            logger2.error("", e);
        }
    }

    //0点的打印提前到59分
    private static boolean isMinuteGap(int curMinute) {
        return ((curMinute % 10 == 0 && curMinute != 0) || curMinute == 59) && lastMinute != curMinute;
    }

}